<?php
		
	/* SVN FILE: $Id: toggle.php 8 2011-03-13 08:32:38Z michele.andreoletti@gmail.com $ */
	
	/**
	* Project Name : arcadia
	*
	* @author $Author: michele.andreoletti@gmail.com $
	* @version $Revision: 8 $
	* @lastrevision $Date: 2011-03-13 08:32:38 +0000 (Sun, 13 Mar 2011) $
	* @filesource $URL: http://arcadia.googlecode.com/svn/trunk/include/toggle.php $
	*/
			
	date_default_timezone_set('Europe/Rome');

	if(isset($_GET['sAPP']) && !empty($_GET['sAPP'])) {
		$sAPP = $_GET['sAPP'];	
	
	} else {
		require_once "./const.inc.php";
		require_once "./functions.php";
		// require_once "./auth.inc.php";
		require_once "./conn.inc.php";
		
		fnUpdateLog('APP NOT DEFINED', 0, '', 'E');
		echo fnJsonOutput(fnCurrentScript(), '', '', 888);
		die();
	
	}

	require_once "../$sAPP/include/const.inc.php";
	require_once "../$sAPP/include/functions.php";
	require_once "./auth.inc.php";
	require_once "./conn.inc.php";

	fnGetValueFromGET(array('sAction','sField','sValue','iView','sViewItemLink','sTable','sAPP'));
	
	if ((!isset($sViewItemLink) || empty($sViewItemLink)) && !empty($sTable)) {
		fnTableConst($sTable);
	}
		
	fnViewConst($sViewItemLink);
		
	$sScript = fnCurrentScript();
	
	if (!empty($iView)) {
		
		$aData = fnCheckView($iView, $sViewSelectFrom, $sViewSelectGroupBy, 'aData');
	
		if (!empty($aData)) { // esiste un record associato a $iView
			$iAccessLvl = fnViewAccessLvl($iView, $aTABLE, $aTABLE_SECURITY_CHECK);
			$iFieldAccessLvl = fnFieldAccessLvl($iAccessLvl, 0, $sField);
			if (is_allowed('edit', $iFieldAccessLvl)) {
				switch ($sAction) {
					case "toggle":
						$sQuery = "UPDATE `$sViewSelectFrom` SET `$sField`='$sValue' WHERE `id`='$iView'";
						$rResult = custom_mysql_query($sQuery);	
						fnUpdateLog('RECORD EDITED', $iView, $sViewSelectFrom, 'I');
						$sOutput = fnJsonOutput($sScript, $sAction, '', 0, array($sField => $sValue));
						break;
					default:
						fnUpdateLog('ACTION NOT VALID', $iView, $sViewSelectFrom, 'E');
						$sOutput = fnJsonOutput($sScript, $sAction, '', 101);
						break;
				}
			
			} else {
				fnUpdateLog('LOW RIGHTS', $iView, $sViewSelectFrom, 'S');
				$sOutput = fnJsonOutput($sScript, $sAction, '', 999);
			
			}
			
		} else {
			fnUpdateLog('RECORD NOT FOUND', $iView, $sViewSelectFrom, 'E');
			$sOutput = fnJsonOutput($sScript, $sAction, '', 111);
			
		}
	
	} else {
		fnUpdateLog('RECORD ID EMPTY', $iView, $sViewSelectFrom, 'E');
		$sOutput = fnJsonOutput($sScript, $sAction, '', 100);

	}		
	
	echo $sOutput;	
	die();
	
?>